package com.lnking.lnkingplace.service.impl;

import com.lnking.lnkingplace.model.dto.inspect.inspectType.InspectIndexListDto;
import com.lnking.lnkingplace.model.entity.InspectResult;
import com.lnking.lnkingplace.model.entity.InspectType;
import com.lnking.lnkingplace.mapper.InspectTypeMapper;
import com.lnking.lnkingplace.model.request.inspect.inspectType.InspectTypeAddRequest;
import com.lnking.lnkingplace.model.request.inspect.inspectType.InspectTypeUpdateRequest;
import com.lnking.lnkingplace.service.IInspectResultService;
import com.lnking.lnkingplace.service.IInspectTypeService;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sn
 * @since 2022-09-16
 */
@Service
public class InspectTypeServiceImpl extends CustomServiceImpl<InspectTypeMapper, InspectType> implements IInspectTypeService {

    @Autowired
    IInspectResultService iInspectResultService;
    /**
     * 分号隔开
     */
    public static final String SPIT=";";

    @Override
    public void add(InspectTypeAddRequest params) {
        InspectType inspectType = new InspectType();
        BeanUtils.copyProperties(params,inspectType);
        inspectType.setCreateTime(LocalDateTime.now());
        getBaseMapper().insert(inspectType);
    }

    @Override
    public void update(InspectTypeUpdateRequest params) {
        InspectType inspectType = new InspectType();
        BeanUtils.copyProperties(params,inspectType);
        getBaseMapper().updateById(inspectType);
    }



    @Override
    public Map<String,List<InspectIndexListDto>> getContent(String type) {
        Map<String,List<InspectIndexListDto>> listMap= new HashMap<>();
        for (String s : type.split(SPIT)) {
            List<InspectIndexListDto> content = getBaseMapper().getContent(s);
            listMap.put(s,content);
        }
        return listMap;
    }

    @Override
    public List<InspectType> getOutList(String rectificationId, String content,String classification) {
        List<InspectResult> byRectificationId = iInspectResultService.getByRectificationId(rectificationId);
        List<InspectType> inspectTypeList = getBaseMapper().getListByContent(content,classification);
        for (InspectResult inspectResult : byRectificationId) {
            for (int i = 0; i < inspectTypeList.size(); i++) {
                if (inspectTypeList.get(i).getContent().equals(inspectResult.getInspectContent())) {
                    inspectTypeList.remove(inspectTypeList.get(i));
                    i--;
                }
            }
                   }
        return inspectTypeList;
    }
}
